<template>
    <div>
        <Title :title="title"></Title>
        <div ref="right1" style="height: 150px;padding-top: 25px;"></div>
    </div>
</template>
<script>
import { Line } from '@antv/g2plot';
import { get } from '@/utils/request'
import moment from 'moment'
export default {
    data() {
        return {
            title: '实时PM2.5/10双折曲线',
            Data: []
        }
    },
    methods: {
        // 封装加载图表方法
        loadLine() {
             const linePlot = new Line(this.$refs.right1, {
      data:this.Data,
      xField: 'time',
      yField: 'value',
      seriesField: 'name',
      yAxis: {
        label: {
          formatter: (v) => `${v}`,
        },
      },
      legend: {
        position: 'top',
      },
      smooth: true,
      // @TODO 后续会换一种动画方式
      animation: {
        appear: {
          animation: 'path-in',
          duration: 5000,
        },
      },
    });

    linePlot.render();
        },
        // 请求后端数据 
        async getTempData() {
            let res = await get('/dashboard/pageQueryTodayData', { page: 1, pageSize: 100 });
            console.log(res, '获取响应');
            const fields = ["pm10", "pm25"];
            let temp = [];
            res.data.list.forEach((item, index) => {
                // 每隔10个数据取一个
                if (index % 10 === 0) {
                    const formattedTime = moment(item.insert_time).format('HH:mm');
                    fields.forEach(field => {
                        temp.push({
                            name: field,
                            time: formattedTime,
                            value: parseFloat(item[field])
                        });
                    });
                }
            });
            this.Data = temp;
        }
    },
    // 生命周期 vue实例与模板绑定完成 此时可以获取dom元素
    async mounted() {
        // 隐式创建微任务
        await this.getTempData();
        this.loadLine();
    }
}
</script>
<style scoped></style>
